package Test.restful;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import weaver.rsa.security.RSA;

import java.util.HashMap;
import java.util.Map;

public class CreateWorkFlowTest {

        public static String getRegist() {
                //httpclient的使用请自己封装，可参考ECOLOGY中HttpManager类
                //请求头信息封装集合
                Map<String, String> heads = new HashMap<String, String>();
                //获取当前异构系统RSA加密的公钥
                String cpk = new RSA().getRSA_PUB();
                //封装参数到请求头
                heads.put("appid", "EEAA5436-7577-4BE0-8C6C-89E9D88805EA");
                heads.put("cpk", cpk);
                //调用ECOLOGY系统接口进行注册
                String data = HttpClient.httpPostForm("http://221.226.25.34:8991/api/ec/dev/auth/regist", null, heads,"utf-8");
                //返回的数据格式为json，具体格式参数格式请参考文末API介绍。
                //注意此时如果注册成功会返回秘钥信息,请根据业务需要进行保存。
                // System.out.println(data);
                /**********************其它业务逻辑实现***********************/

//        {"msg":"ok","errcode":"0","code":0,"msgShowType":"none","secrit":"395f8822-3de8-4ef2-809a-ee838aa72aad","errmsg":"ok","status":true,"spk":"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsgdJ7luSHNgGUy3kWlF2ZBTJO+VeLyIOSmu5D1Iliv3GnnSakhTUPbGoUAeALmJip42ACKCbe3R7POJ9e2JoDD7dzXgRaOzX3gWgejFk2gp8L9stfe7J6JjSQewWk7y/liyQ9ddJ0iK2lNjqUZPGuz03jCfmUS+nM1S6jXgYAAKNHMJijVd8a/G92cOsCvUKQQndw7r4ZkCLY/G6ipXLXTegs/F/72JRuWQcw3uk59tiX15og+F7ulzcxhbmVj3GnNmkHPIrLOm6ExmsYikBL8X0h8HPcaV9Kk6pCRmxc06/o450nbtIHWjDVBuGZxJv83Hrg15yBnbFu50xvx31uwIDAQAB"}

                return data;
        }


        public static String  applytoken(String secrit,String spk)  {
                //请求头信息封装集合
                Map<String, String> heads = new HashMap<String, String>();
                //ECOLOGY返回的系统公钥
                RSA rsa = new RSA();
                //对秘钥进行加密传输，防止篡改数据
                String secret_2 = rsa.encrypt(null, secrit, null, "utf-8", spk, false);
                //封装参数到请求头
                heads.put("appid", "EEAA5436-7577-4BE0-8C6C-89E9D88805EA");
                heads.put("secret", secret_2);
                //调用ECOLOGY系统接口进行申请
                String data = HttpClient.httpPostForm("http://221.226.25.34:8991/api/ec/dev/auth/applytoken", null, heads,"utf-8");
                //返回的数据格式为json，具体格式参数格式请参考文末API介绍。
                //注意此时如果申请成功会返回token,请根据业务需要进行保存。
             //   System.out.println(data);
                /**********************其它业务逻辑实现***********************/
                return data;
        }

    public static void main(String[] args){


            String back =  getRegist();
            JSONObject object = JSONObject.fromObject(back);
            String secrit = object.getString("secrit");
            String spk = object.getString("spk");
            String bak2 = applytoken(secrit,spk);
            JSONObject objecttoken = JSONObject.fromObject(bak2);
            String token = objecttoken.getString("token");
            Map<String, String> heads=new HashMap<String, String>();
            //接口参数信息封装集合
            Map<String, Object> param=new HashMap<String, Object>();
            RSA rsa = new RSA();
            //对用户真实系统id进行加密传输，防止篡改数据
            String userid = rsa.encrypt(null, "1", null, "utf8", spk, false);
            //封装参数到请求头
            heads.put("token", token);
            heads.put("appid", "EEAA5436-7577-4BE0-8C6C-89E9D88805EA");
            heads.put("userid", userid);

            String url = "http://221.226.25.34:8991/api/workflow/paService/doCreateRequest";
            Map mainMap = new HashMap();
            JSONArray mainFieldjonsArray = new JSONArray();
            JSONObject mainFieldJson1 = new JSONObject();
            mainFieldJson1.put("fieldName","xm");
            mainFieldJson1.put("fieldValue","张三");
            mainFieldjonsArray.add(mainFieldJson1);

            JSONObject mainFieldJson2 = new JSONObject();
            mainFieldJson2.put("fieldName","nl");
            mainFieldJson2.put("fieldValue","20");
            mainFieldjonsArray.add(mainFieldJson2);

            JSONObject mainFieldJson3 = new JSONObject();
            mainFieldJson3.put("fieldName","dh");
            mainFieldJson3.put("fieldValue","123456789");
            mainFieldjonsArray.add(mainFieldJson3);

            JSONObject mainFieldJson4 = new JSONObject();
            mainFieldJson4.put("fieldName","zz");
            mainFieldJson4.put("fieldValue","南京");
            mainFieldjonsArray.add(mainFieldJson4);

            JSONObject mainFieldJson5 = new JSONObject();
            mainFieldJson5.put("fieldName","zp");
            JSONArray zpjonsArray = new JSONArray();
            JSONObject zpjson = new JSONObject();
            zpjson.put("filePath","https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1577426926378&di=0084fc19f5cb90fc2286aab5ca8c343e&imgtype=0&src=http%3A%2F%2Fpic.51yuansu.com%2Fpic2%2Fcover%2F00%2F41%2F80%2F581369c715701_610.jpg");
            zpjson.put("fileName","timg.jpg");
            zpjonsArray.add(zpjson);
            mainFieldJson5.put("fieldValue",zpjonsArray.toString());
            mainFieldjonsArray.add(mainFieldJson5);


            //明细表字段
            JSONArray dtlFieldjonsArray = new JSONArray();
            JSONObject dtlFieldJson1 = new JSONObject();
            dtlFieldJson1.put("fieldName","jkzk");
            dtlFieldJson1.put("fieldValue","良好");
            dtlFieldjonsArray.add(dtlFieldJson1);

            JSONObject dtlFieldJson2 = new JSONObject();
            dtlFieldJson2.put("fieldName","hyzk");
            dtlFieldJson2.put("fieldValue","未婚");
            dtlFieldjonsArray.add(dtlFieldJson2);

            JSONObject dtlFieldJson3 = new JSONObject();
            dtlFieldJson3.put("fieldName","jtqk");
            dtlFieldJson3.put("fieldValue","父母健在，兄弟姐妹和睦");
            dtlFieldjonsArray.add(dtlFieldJson3);

            JSONObject dtlFieldJson4 = new JSONObject();
            dtlFieldJson4.put("fieldName","shbj");
            dtlFieldJson4.put("fieldValue","良好市民");
            dtlFieldjonsArray.add(dtlFieldJson4);
            //明细表记录数
            JSONArray dtlRecordsjonsArray = new JSONArray();
            JSONObject recodeJson = new JSONObject();
            recodeJson.put("recordOrder",0);
            recodeJson.put("workflowRequestTableFields",dtlFieldjonsArray.toString());
            dtlRecordsjonsArray.add(recodeJson);
            //明细表1
            JSONArray dtlTablejonsArray = new JSONArray();
            JSONObject dtlTableJson = new JSONObject();
            dtlTableJson.put("tableDBName","formtable_main_25_dt1");
            dtlTableJson.put("workflowRequestTableRecords",dtlRecordsjonsArray.toString());
            dtlTablejonsArray.add(dtlTableJson);

            mainMap.put("mainData",mainFieldjonsArray.toString());
            mainMap.put("detailData",dtlTablejonsArray.toString());
            mainMap.put("requestName","API测试添加附件-杜松松-05");
            mainMap.put("workflowId","6");

            JSONObject jsonObject = JSONObject.fromObject(mainMap);
            System.out.println(jsonObject.toString());

            String back1 = HttpClient.httpPostForm(url,mainMap,heads,"utf-8");
            System.out.println(back1);

    }
}
